package com.ddd.demo.lee.test;

public class Solution7 {

    public static int reverse2(int x) {
        int rev = 0;
        while (x != 0){
            if(rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10){
                return 0;
            }

            int digit = x % 10;
            x /= 10;
            rev = rev * 10 + digit;
        }

        return rev;
    }

    public static int reverse(int x) {
        String s = x + "";
        String r = "";
        char[] chars = s.toCharArray();
        boolean flag = false;
        if (chars[0] == '-') {
            flag = true;
            chars = s.substring(1, s.length()).toCharArray();
        }
        for (int i = chars.length - 1; i >= 0 ; i--) {
            r += chars[i];
        }

        if(flag){
            return  0 - new Integer(r);
        }else {
            return new Integer(r);
        }
    }

    public static void main(String[] args) {
        int i = 1534236469;
        int reverse = reverse2(i);
        System.out.println("reverse = " + reverse);
    }
}
